package h2;

import org.h2.tools.RunScript;
import org.h2.tools.Script;

import java.io.FileReader;
import java.sql.Connection;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class SQLTools {
    public static void main(String[] args) {
        // 方式2：使用 Java 8 DateTimeFormatter（推荐）
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分");
        String dateTimeStr = LocalDateTime.now().format(formatter);
        System.out.println("当前时间: " + dateTimeStr); // 输出: 202312151430

        //导出数据库
        try {
            exportDatabase("/Users/tangxinhui/work/code/OA/renren-security/renren-admin/db/h2/backup/"+dateTimeStr+" 备份.sql");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    // 导出数据库
    public static void exportDatabase(String file) throws Exception {
        Script.process("jdbc:h2:/Users/tangxinhui/work/code/OA/renren-security/renren-admin/db/h2/h2;MODE=MySQL;DATABASE_TO_LOWER=TRUE;CASE_INSENSITIVE_IDENTIFIERS=TRUE;AUTO_SERVER=TRUE;", "sa", "", file, "", "");
    }

    // 导入数据库
    public static void importDatabase() throws Exception {
        RunScript.execute("jdbc:h2:file:~/newdb", "sa", "", "backup.sql", null, false);
    }

    // 使用Connection导入
    public static void importWithConnection(Connection conn, String scriptFile) throws Exception {
        RunScript.execute(conn, new FileReader(scriptFile));
    }
}